package com.safebox.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.safebox.R;
import com.safebox.database.DatabaseHandler;
import com.safebox.database.DatabaseHelper;
import com.safebox.database.model.Message;
import com.safebox.security.DataSecurity;

public class ItemContentActivity extends Activity {
    private EditText title;
    private EditText content;
    private TextView updateDate;
    private TextView createDate;
    
    private long msg_id;
    
    private String titleStr;
    private String contentStr;
    
    private DatabaseHandler db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.screen_content);
        
        msg_id = getIntent().getLongExtra("id", 0xff);
        if (msg_id < 0)
            return;
        title = (EditText) findViewById(R.id.edit_item_title);
        content = (EditText) findViewById(R.id.edit_item_content);
        updateDate = (TextView) findViewById(R.id.view_item_date_modified);
        createDate = (TextView) findViewById(R.id.view_item_date_created);
        
        db = DatabaseHandler.getInstance(this);
        String select = DatabaseHelper.MessagesColumns.MASSAGE_ID + "=" + msg_id;
        Cursor cur = db.query(DatabaseHelper.Tables.MESSAGES, null, select, null, null, null, null);
        startManagingCursor(cur);
        
        
        if (cur.moveToFirst()) {
            titleStr = cur.getString(cur.getColumnIndex(DatabaseHelper.MessagesColumns.TITLE));
            contentStr = DataSecurity.desDecrypt(cur.getString(cur.getColumnIndex(DatabaseHelper.MessagesColumns.CONTENT)));
            title.setText(titleStr);
            content.setText(contentStr);
            updateDate.setText(cur.getString(cur.getColumnIndex(DatabaseHelper.MessagesColumns.DATE_MODIFIED)));
            createDate.setText(cur.getString(cur.getColumnIndex(DatabaseHelper.MessagesColumns.DATE_CREATED)));
        }

        
    }
    
    @Override
    public void onBackPressed() {       
        String select = DatabaseHelper.MessagesColumns.MASSAGE_ID + "=" + msg_id;
        Cursor cur = db.query(DatabaseHelper.Tables.MESSAGES, null, select, null, null, null, null);
        if (cur.moveToNext()) {
            titleStr = cur.getString(cur.getColumnIndex(DatabaseHelper.MessagesColumns.TITLE));
            contentStr = DataSecurity.desDecrypt(cur.getString(cur
                    .getColumnIndex(DatabaseHelper.MessagesColumns.CONTENT)));
        }
        cur.close();
        if (TextUtils.equals(titleStr, title.getText().toString()) && TextUtils.equals(contentStr, content.getText().toString())) {
            super.onBackPressed();
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.str_save_prompt)
        .setPositiveButton(R.string.str_yes, new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {                
                changeListItem(msg_id, title.getText().toString(), content.getText().toString());
                dialog.cancel();
                ItemContentActivity.this.finish();
            }
        })
        .setNegativeButton(R.string.str_no, new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                ItemContentActivity.this.finish();
            }
        })
        .create().show();        
    }
    
    private void changeListItem(long index, String title, String content) {
        Message msg = new Message();
        msg.setTitle(title);
        msg.setContent(content);

        db.update(DatabaseHelper.Tables.MESSAGES, msg, index);
    }
    
    private void removeListItem(long item) {
        DatabaseHandler.getInstance(this).delete(DatabaseHelper.Tables.MESSAGES, item);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.item_content_option_menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_save:
                changeListItem(msg_id, title.getText().toString(), content.getText().toString());
                return true;
            case R.id.menu_delete:
                removeListItem(msg_id);
                finish();
                return true;
            case R.id.menu_send_msg:
                String tt= getResources().getString(R.string.str_title) + ":" + title.getText().toString() + "\n";
                String cc = getResources().getString(R.string.str_content) + ":" + content.getText().toString(); 
                sendMessage(tt + cc);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }
    
    private void sendMessage(String body) {
        Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:"));
        smsIntent.putExtra("sms_body", body);
        startActivity(smsIntent);
    }
    
    

}
